﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.Infrastructure;
using System.Data.Entity;

namespace Providers
{
    public static class DbSetExtensions
    {
        public static TEntity SoftFind<TEntity>(this DbSet<TEntity> dbSet, object id)
            where TEntity : class, ISoftDeleteEntity
        {
            var entity = dbSet.Find(id);

            if (entity == null || entity.Deleted)
                return default(TEntity);

            return entity;
        }

        public static IQueryable<TEntity> SoftList<TEntity>(this DbSet<TEntity> dbSet)
            where TEntity : class, ISoftDeleteEntity
        {
            return dbSet.Where(x => !x.Deleted);
        }
    }

    public static class DbQueryExtensions
    {
        public static IQueryable<TEntity> SoftList<TEntity>(this DbQuery<TEntity> dbSet)
            where TEntity : class, ISoftDeleteEntity
        {
            return dbSet.Where(x => !x.Deleted);
        }
    }
}
